import {
    request
} from "../../../request/index.js";
var redis = require("../../../utils/redis.js").default;
Page({
    data: {
        consigneesList: []
    },
    onShow: function (options) {
        this.getConsigneesList();
    },
    addressAddOrUpdate(event) {
        wx.navigateTo({
            url: '/pages/ucenter/addressAdd/index?id=' + event.currentTarget.dataset.addressId,
        })
    },
    selectAddress(event) {
        try {
            wx.setStorageSync('addressId', event.currentTarget.dataset.addressId);
        } catch (e) {
        }
        //选择该收货地址
        wx.navigateBack({
            url: '/pages/pay/index'
        });
    },
    async getConsigneesList() {
        const userId = redis.get('userId');
        const res = await request({
            url: "/consignees/query",
            data: {
                userId
            }
        })
        this.setData({
            consigneesList: res.data.data
        })
    },
    deleteAddress(event) {
        wx.showModal({
            title: '',
            content: '确定要删除地址？',
            success: async (e) => {
                if (e.confirm) {
                    const id = event.target.dataset.addressId;
                    const res = await request({
                        url: "/consignees/delConsignee",
                        method: "post",
                        data: id
                    });
                    if (res.data.code == 200) {
                        this.getConsigneesList();
                    } else {
                        util.showErrorToast(res.data.data);
                    }

                    // util.request(api.AddressDelete, {
                    //     id: addressId
                    // }, 'POST', 'application/json').then(function (res) {
                    //     if (res.errno === 0) {
                    //         that.getAddressList();
                    //     }
                    // });
                }
            }
        })
        return false;

    },
})